﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Daabli;

namespace ConsoleApp
{
    public enum Master
    {
        Tigress, Monkey, Mantis, Viper, Crane
    };

    class Program
    {
        #region Helpers
        private static Stream GetFileStream(string filename)
        {
            return new FileStream(filename, FileMode.Open, FileAccess.Read);
        }
        public static bool LoadFile(Reader r, string filename)
        {
            if (string.IsNullOrEmpty(filename))
            {
                r.Log.Write(MessageType.InvalidArgument, "filename cannot be empty");
                return false;
            }

            Stream stream = null;
            Reader.NoThrowAction(() => { stream = GetFileStream(filename); });

            if (stream == null)
            {
                r.Log.Write(MessageType.Error, "failed to open file '{0}'", filename);
                return false;
            }

            using (stream)
                return r.FromStream(stream);
        }
        private static string StringFrom<T>(List<T> list)
        {
            if (list.Count <= 0)
                return string.Empty;

            var result = new StringBuilder();

            result.Append(list[0]);
            for (int i = 1; i < list.Count; ++i)
            {
                result.Append(", ");
                result.Append(list[i]);
            }

            return result.ToString();
        }
        #endregion

        static void Main(string[] args)
        {
            // create a reader
            var r = new DefaultReader();

            // load data from file
            if (LoadFile(r, "data.txt"))
            {
                bool isSmiling = false;
                char weaponClass = 'A';
                float speed = 0;
                int pixelCount = 0;
                string desc = null;
                float agility = 0;
                Master master = Master.Tigress;
                List<string> tags = new List<string>();
                Creature creature = null;
                List<string> userNames = new List<string>();
                string currentUser = null;

                // register custom type names
                r.RegisterTypeName<Master>();
                r.RegisterTypeName<Wolf>();

                // read data
                bool result =
                    r.Read("isSmiling", ref isSmiling) &&
                    r.Read("weaponClass", ref weaponClass) &&
                    r.Read("speed", ref speed) &&
                    r.Read("pixelCount", ref pixelCount) &&
                    r.Read("desc", ref desc) &&
                    r.Read("agility", ref agility, 1.2f) &&
                    r.Read("master", ref master) &&
                    r.Read("tags", ref tags) &&
                    r.Read("creature", ref creature) &&
                    r.Read("userNames", ref userNames) &&
                    r.Read("currentUser", ref currentUser);

                // show data
                if (result)
                {
                    Console.WriteLine("isSmiling: {0}", isSmiling);
                    Console.WriteLine("weaponClass: {0}", weaponClass);
                    Console.WriteLine("speed: {0}", speed);
                    Console.WriteLine("pixelCount: {0}", pixelCount);
                    Console.WriteLine("desc: {0}", desc);
                    Console.WriteLine("agility: {0}", agility);
                    Console.WriteLine("master: {0}", master);
                    Console.WriteLine("tags: {0}", StringFrom(tags));
                    Console.WriteLine("creature: {0}", creature);
                    Console.WriteLine("userNames: {0}", StringFrom(userNames));
                    Console.WriteLine("currentUser: {0}", currentUser);
                }
            }

            // show log
            var resultText = new StringBuilder();
            {
                resultText.Append("log: ");

                var log = r.Log.ToString();
                if (!string.IsNullOrEmpty(log))
                {
                    resultText.AppendLine();
                    resultText.Append(log);
                }
                else
                    resultText.Append("empty");
            }
            Console.WriteLine();
            Console.WriteLine(resultText.ToString());
        }
    }
}
